﻿using MediatR;
using W.Warehouse.Api.Application.Command.OutboundCommand;
using W.Warehouse.Domain;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Api.Application.CommandHandlder.OutboundCommandHadler
{
    public class OutboundUpdateCommandHandler:IRequestHandler<OutboundUpdateCommand, int>
    {
        private readonly IOutboundRepository outboundRepository;

        private readonly IOutStorageProductsRepository productsRepository;

        public OutboundUpdateCommandHandler(IOutboundRepository outboundRepository, IOutStorageProductsRepository productsRepository)
        {
            this.outboundRepository = outboundRepository;
            this.productsRepository = productsRepository;
        }

        public async Task<int> Handle(OutboundUpdateCommand request, CancellationToken cancellationToken)
        {
            int row = 0;
            Outbound outbound = new Outbound();
            outbound.Outboundid = request.Outboundid;
            outbound.OutboundNo = request.OutboundNo;
            outbound.OutboundType = request.OutboundType;
            outbound.AssociateNumber = request.AssociateNumber;
            outbound.CustomerNo = request.CustomerNo;
            outbound.CustomerName = request.CustomerName;
            outbound.CustomerContact = request.CustomerContact;
            outbound.CustomerPhone = request.CustomerPhone;
            outbound.SingleMaker = request.SingleMaker;
            outbound.SingleTime = request.SingleTime;
            outbound.OutboundRemarks = request.OutboundRemarks;
            outbound.CustomerPhone = request.CustomerPhone;
            outbound.VendorNo = request.VendorNo;
            outbound.VendorName = request.VendorName;
            outbound.VendorContact = request.VendorContact;
            outbound.VendorPhone = request.VendorPhone;
            outbound.IsDelete = false;
            row += await outboundRepository.UpdateAsync(outbound);
            List<OutStorageProducts> products = new List<OutStorageProducts>();
            foreach (var item in request.products)
            {
                OutStorageProducts outStorageProducts = new OutStorageProducts();
                outStorageProducts.Outboundid = outbound.Outboundid;
                outStorageProducts.ProductName = item.ProductName;
                outStorageProducts.ProductNo = item.ProductNo;
                outStorageProducts.ProductSpec = item.ProductSpec;
                outStorageProducts.Batch = item.Batch;
                outStorageProducts.Price = item.Price;
                outStorageProducts.OutStorageNum = item.OutStorageNum;
                outStorageProducts.TotalPrice = item.TotalPrice;
                outStorageProducts.BinLocationName = item.BinLocationName;
                outStorageProducts.office_id = item.office_id;
                outStorageProducts.user_id = item.user_id;
                products.Add(outStorageProducts);
            }
            row += await productsRepository.UpdateRangeAsync(products);
            return row;
        }
    }
}
